package com.ptswitch.material.web.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;

import com.ptswitch.material.model.Upload;
import com.ptswitch.material.util.DateUtils;

@Controller("uploadAction")
public class UploadAction extends ActionSupport{

	protected File[] files;
	protected String[] filesFileName;
	protected String[] filesContentType;
	protected List<Upload> uploads = new ArrayList<Upload>();
	protected Upload upload;

	protected String ownId;
	protected String ownType;

	public String toUpload() {
		mustAdmins();
		return "upload";
	}

	protected void uploads() {
		uploads.clear();
		if (files == null) {
			return;
		}
		for (int i = 0; i < files.length; i++) {
			String newFileName = generateFileName(this.filesFileName[i]);
			try {
				FileUtils.copyFile(files[i], new File(ActionSupport.getSavePath() + newFileName), true);
				upload = new Upload();
				upload.setFileName(filesFileName[i]);
				upload.setFilePath(newFileName);
				upload.setFileType(filesContentType[i]);
				upload.setCreateTime(DateUtils.format(new Date()));
				upload.setUser(visitor());
				uploads.add(upload);
				logger.debug("upload file {} ,{}", upload.getFileName(), upload.getFilePath());
			} catch (IOException e) {
				logger.error("上传文件出错", e);
			}
		}
		files = null;
	}

	protected String generateFileName(String fileName) {
		DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		String formatDate = format.format(new Date());

		int position = fileName.lastIndexOf(".");
		String extension = fileName.substring(position);
		return fileName + formatDate + extension;
	}

	public String download() {
		upload = uploadSer.get(id);
		return "download";
	}

	public InputStream getTargetFile() {
		InputStream targetFile = null;
		try {
			targetFile = FileUtils.openInputStream(new File(ActionSupport.getSavePath() + upload.getFilePath()));
		} catch (Exception e) {
			logger.error("文件不存在:{}", e.getMessage());
		}
		if (targetFile == null) {
			targetFile = IOUtils.toInputStream("文件已被删除");
			this.upload = new Upload();
			this.upload.setFileName("notexist.txt");
			this.upload.setFileType("text/plain");
		}
		return targetFile;
	}

	// get and set
	public File[] getFiles() {
		return files;
	}

	public void setFiles(File[] files) {
		this.files = files;
	}

	public String[] getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}

	public String[] getFilesContentType() {
		return filesContentType;
	}

	public void setFilesContentType(String[] filesContentType) {
		this.filesContentType = filesContentType;
	}

	public Upload getUpload() {
		return upload;
	}

	public String getOwnType() {
		return ownType;
	}

	public void setOwnType(String ownType) {
		this.ownType = ownType;
	}

	public String getOwnId() {
		return ownId;
	}

	public void setOwnId(String ownId) {
		this.ownId = ownId;
	}

}
